package com.msh.artascope.devcloud.service.api;

import com.msh.artascope.devcloud.service.model.query.DefalutResourceQueryVo;
import com.msh.artascope.devcloud.service.model.vo.DefalutResourceVo;
import com.msh.frame.client.common.Result;
import com.msh.frame.client.exception.BizException;
import com.msh.frame.client.validation.*;
import com.msh.artascope.interfaces.IAccountGet;
import com.msh.artascope.interfaces.ILoginAccount;
import com.msh.frame.web.base.BaseController;
import com.msh.frame.web.util.HttpServletRequestUtil;
import com.msh.artascope.devcloud.client.po.DefalutResourcePo;
import com.msh.artascope.devcloud.client.qo.DefalutResourceQo;
import com.msh.artascope.devcloud.service.service.DefalutResourceService;
import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.groups.Default;
import java.util.List;


/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Tag(name = "默认资源-api相关接口")
@Slf4j
@RestController
@RequestMapping("/api/defalutResource")
public class DefalutResourceApi extends BaseController<DefalutResourcePo, DefalutResourceQo> {

  @Autowired
  private DefalutResourceService defalutResourceService;

  @Autowired
	private IAccountGet accountGet;


  @Operation(summary = "通用插入逻辑")
  @PostMapping("insert")
  public Result<Long> insert(@Validated({IInsertVo.class,Default.class}) @RequestBody DefalutResourceVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    return Result.successReturn(defalutResourceService.insert(p));
  }

  @Operation(summary = "通用更新逻辑")
  @PostMapping("update")
  public Result update(@Validated({IUpdateVo.class,Default.class}) @RequestBody DefalutResourceVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    defalutResourceService.update(p);
    return Result.successReturn();
  }


  @Operation(summary = "通用删除逻辑")
  @GetMapping("delete")
  public Result delete(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    defalutResourceService.delete(id, loginAccount.getTenantId());
    return Result.successReturn();
  }

  @Operation(summary = "获取详情")
  @GetMapping("get")
  public Result<DefalutResourceVo> get(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(defalutResourceService.getVo(id, loginAccount.getTenantId()));
  }

  @Operation(summary = "vo列表查询")
  @PostMapping("list")
  public Result<List<DefalutResourceVo>> list(@Validated({IListVo.class}) @RequestBody DefalutResourceQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return Result.successReturn(defalutResourceService.listVo(q));
  }


  @Operation(summary = "vo列表分页查询")
  @PostMapping("page")
  public Result<List<DefalutResourceVo>> page(@Validated({IListVo.class}) @RequestBody DefalutResourceQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return defalutResourceService.pageVo(q);
  }
}
